package com.nmwlw.demo.ui

import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.activity.compose.BackHandler
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.viewinterop.AndroidView

@Composable
fun WebViewScreen() {
    val context = LocalContext.current
    var webView by remember { mutableStateOf<WebView?>(null) }

    AndroidView(
        factory = { ctx ->
            WebView(ctx).apply {
                settings.javaScriptEnabled = true // 启用 JS
                webViewClient = WebViewClient()
                loadUrl("https://baidu.com") // 替换为你的 URL
                webView = this
            }
        },
        modifier = Modifier.fillMaxSize(),
        update = { view ->
            // 可在此处更新 WebView 配置
        }
    )

    // 处理生命周期
    DisposableEffect(Unit) {
        onDispose {
            webView?.destroy() // 防止内存泄漏
        }
    }
}